home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-blog-header.php < prev    next >
Encoding:
PHP Script  |  2005-05-06  |  8.3 KB  |  259 lines

  1. <?php
  2.  
  3. if (! isset($wp_did_header)):
  4. if ( !file_exists( dirname(__FILE__) . '/wp-config.php') ) {
  5.     if ( strstr( $_SERVER['PHP_SELF'], 'wp-admin') ) $path = '';
  6.     else $path = 'wp-admin/';
  7.     die("There doesn't seem to be a <code>wp-config.php</code> file. I need this before we can get started. Need more help? <a href='http://wordpress.org/docs/faq/#wp-config'>We got it</a>. You can <a href='{$path}setup-config.php'>create a <code>wp-config.php</code> file through a web interface</a>, but this doesn't work for all server setups. The safest way is to manually create the file.");
  8. }
  9.  
  10. $wp_did_header = true;
  11.  
  12. require_once( dirname(__FILE__) . '/wp-config.php');
  13.  
  14. $query_vars = array();
  15.  
  16. // Process PATH_INFO and 404.
  17. if ((isset($_GET['error']) && $_GET['error'] == '404') ||
  18.         ((! empty($_SERVER['PATH_INFO'])) &&
  19.         ('/' != $_SERVER['PATH_INFO']) &&
  20.          (false === strpos($_SERVER['PATH_INFO'], '.php'))
  21.         )) {
  22.  
  23.     // If we match a rewrite rule, this will be cleared.
  24.     $error = '404';
  25.  
  26.     // Fetch the rewrite rules.
  27.     $rewrite = $wp_rewrite->wp_rewrite_rules();
  28.  
  29.     if (! empty($rewrite)) {
  30.         $pathinfo = $_SERVER['PATH_INFO'];
  31.         $req_uri = $_SERVER['REQUEST_URI'];      
  32.         $home_path = parse_url(get_settings('home'));
  33.         $home_path = $home_path['path'];
  34.  
  35.         // Trim path info from the end and the leading home path from the
  36.         // front.  For path info requests, this leaves us with the requesting
  37.         // filename, if any.  For 404 requests, this leaves us with the
  38.         // requested permalink.    
  39.         $req_uri = str_replace($pathinfo, '', $req_uri);
  40.         $req_uri = str_replace($home_path, '', $req_uri);
  41.         $req_uri = trim($req_uri, '/');
  42.         $pathinfo = trim($pathinfo, '/');
  43.  
  44.         // The requested permalink is in $pathinfo for path info requests and
  45.         //  $req_uri for other requests.
  46.         if (! empty($pathinfo)) {
  47.             $request = $pathinfo;
  48.         } else {
  49.             $request = $req_uri;
  50.         }
  51.  
  52.         // Look for matches.
  53.         $request_match = $request;
  54.         foreach ($rewrite as $match => $query) {
  55.             // If the requesting file is the anchor of the match, prepend it
  56.             // to the path info.
  57.         if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0)) {
  58.           $request_match = $req_uri . '/' . $request;
  59.         }
  60.  
  61.             if (preg_match("!^$match!", $request_match, $matches)) {
  62.                 // Got a match.
  63.                 // Trim the query of everything up to the '?'.
  64.                 $query = preg_replace("!^.+\?!", '', $query);
  65.  
  66.                 // Substitute the substring matches into the query.
  67.                 eval("\$query = \"$query\";");
  68.  
  69.                 // Parse the query.
  70.                 parse_str($query, $query_vars);
  71.  
  72.                 // If we're processing a 404 request, clear the error var
  73.                 // since we found something.
  74.                 if (isset($_GET['error'])) {
  75.                     unset($_GET['error']);
  76.                 }
  77.  
  78.                 if (isset($error)) {
  79.                     unset($error);
  80.                 }
  81.  
  82.                 break;
  83.             }
  84.         }
  85.     }
  86.  }
  87.  
  88. $wpvarstoreset = array('m','p','posts','w', 'cat','withcomments','s','search','exact', 'sentence', 'debug', 'calendar','page','paged','more','tb', 'pb','author','order','orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup');
  89.  
  90. $wpvarstoreset = apply_filters('query_vars', $wpvarstoreset);
  91.  
  92. for ($i=0; $i<count($wpvarstoreset); $i += 1) {
  93.     $wpvar = $wpvarstoreset[$i];
  94.     if (!isset($$wpvar)) {
  95.         if (empty($_POST[$wpvar])) {
  96.             if (empty($_GET[$wpvar]) && empty($query_vars[$wpvar])) {
  97.                 $$wpvar = '';
  98.             } elseif (!empty($_GET[$wpvar])) {
  99.                 $$wpvar = $_GET[$wpvar];
  100.             } else {
  101.                 $$wpvar = $query_vars[$wpvar];
  102.             }
  103.         } else {
  104.             $$wpvar = $_POST[$wpvar];
  105.         }
  106.     }
  107. }
  108.  
  109. // Sending HTTP headers
  110.  
  111. if ( !empty($error) && '404' == $error ) {
  112.     if ( preg_match('/cgi/', php_sapi_name()) )
  113.         @header('Status: 404 Not Found');
  114.     else
  115.         @header('HTTP/1.x 404 Not Found');
  116.  } else if ( empty($feed) ) {
  117.     @header('X-Pingback: '. get_bloginfo('pingback_url'));
  118.     @header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
  119. } else {
  120.     // We're showing a feed, so WP is indeed the only thing that last changed
  121.     if ( $withcomments )
  122.         $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastcommentmodified('GMT'), 0).' GMT';
  123.     else 
  124.         $wp_last_modified = mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0).' GMT';
  125.     $wp_etag = '"' . md5($wp_last_modified) . '"';
  126.     @header("Last-Modified: $wp_last_modified");
  127.     @header("ETag: $wp_etag");
  128.     @header('X-Pingback: ' . get_bloginfo('pingback_url'));
  129.  
  130.     // Support for Conditional GET
  131.     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) $client_last_modified = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
  132.     else $client_last_modified = false;
  133.     if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
  134.     else $client_etag = false;
  135.  
  136.     if ( ($client_last_modified && $client_etag) ?
  137.         ((strtotime($client_last_modified) >= strtotime($wp_last_modified)) && ($client_etag == $wp_etag)) :
  138.         ((strtotime($client_last_modified) >= strtotime($wp_last_modified)) || ($client_etag == $wp_etag)) ) {
  139.         if ( preg_match('/cgi/',php_sapi_name()) ) {
  140.             header('Status: 304 Not Modified');
  141.             echo "\r\n\r\n";
  142.             exit;
  143.         } else {
  144.             if ( version_compare(phpversion(), '4.3.0', '>=') )
  145.                 header('Not Modified', TRUE, 304);
  146.             else
  147.                 header('HTTP/1.x 304 Not Modified');
  148.             exit;
  149.         }
  150.     }
  151. }
  152.  
  153. $use_gzipcompression = get_settings('gzipcompression');
  154.  
  155. $more_wpvars = array('posts_per_page', 'posts_per_archive_page', 'what_to_show', 'showposts', 'nopaging');
  156.  
  157. // Construct the query string.
  158. $query_string = '';
  159. foreach (array_merge($wpvarstoreset, $more_wpvars) as $wpvar) {
  160.     if ($$wpvar != '') {
  161.         $query_string .= (strlen($query_string) < 1) ? '' : '&';
  162.         $query_string .= $wpvar . '=' . rawurlencode($$wpvar);
  163.     }
  164. }
  165.  
  166. $query_string = apply_filters('query_string', $query_string);
  167.  
  168. update_category_cache();
  169. get_currentuserinfo();
  170.  
  171. // Call query posts to do the work.
  172. $posts = & query_posts($query_string);
  173.  
  174. // Extract updated query vars back into global namespace.
  175. extract($wp_query->query_vars);
  176.  
  177. if ( is_single() || is_page() ) {
  178.     $more = 1;
  179.     $single = 1;
  180. }
  181.  
  182. // Issue a 404 if a permalink request doesn't match any posts.  Don't issue a
  183. // 404 if one was already issued, if the request was a search, or if the
  184. // request was a regular query string request rather than a permalink request.
  185. if ( (0 == count($posts)) && !is_404() && !is_search()
  186.         && ( isset($rewrite) || (!empty($_SERVER['QUERY_STRING']) &&
  187.         (false === strpos($_SERVER['REQUEST_URI'], '?'))) ) ) {
  188.     $wp_query->is_404 = true;
  189.     if ( preg_match('/cgi/', php_sapi_name()) )
  190.         @header('Status: 404 Not Found');
  191.     else
  192.         @header('HTTP/1.x 404 Not Found');
  193. }
  194.  
  195. if ($pagenow != 'post.php' && $pagenow != 'edit.php') {
  196.     if ( get_settings('gzipcompression') ) 
  197.         gzip_compression();
  198. }
  199.  
  200. // Template redirection
  201. if ( defined('WP_USE_THEMES') && constant('WP_USE_THEMES') ) {
  202.     do_action('template_redirect');
  203.     if ( is_feed() && empty($doing_rss) ) {
  204.         include(ABSPATH . '/wp-feed.php');
  205.         exit;
  206.     } else if ( is_trackback() && empty($doing_trackback) ) {
  207.         include(ABSPATH . '/wp-trackback.php');
  208.         exit;
  209.     } else if ( is_404() && get_404_template() ) {
  210.         include(get_404_template());
  211.         exit;
  212.     } else if ( is_search() && get_search_template() ) {
  213.         include(get_search_template());
  214.         exit;
  215.     } else if ( is_home() && get_home_template() ) {
  216.         include(get_home_template());
  217.         exit;
  218.     } else if ( is_single() && get_single_template() ) {
  219.         include(get_single_template());
  220.         exit;
  221.     } else if ( is_page() && get_page_template() ) {
  222.         include(get_page_template());
  223.         exit;
  224.     } else if ( is_category() && get_category_template()) {
  225.         include(get_category_template());
  226.         exit;        
  227.     } else if ( is_author() && get_author_template() ) {
  228.         include(get_author_template());
  229.         exit;
  230.     } else if ( is_date() && get_date_template() ) {
  231.         include(get_date_template());
  232.         exit;
  233.     } else if ( is_archive() && get_archive_template() ) {
  234.         include(get_archive_template());
  235.         exit;
  236.     } else if ( is_comments_popup() && get_comments_popup_template() ) {
  237.         include(get_comments_popup_template());
  238.         exit;
  239.     } else if ( is_paged() && get_paged_template() ) {
  240.         include(get_paged_template());
  241.         exit;
  242.     } else if ( file_exists(TEMPLATEPATH . "/index.php") ) {
  243.         include(TEMPLATEPATH . "/index.php");
  244.         exit;
  245.     }
  246. } else {
  247.     // Process feeds and trackbacks even if not using themes.
  248.     if ( is_feed() && empty($doing_rss) ) {
  249.         include(ABSPATH . '/wp-feed.php');
  250.         exit;
  251.     } else if ( is_trackback() && empty($doing_trackback) ) {
  252.         include(ABSPATH . '/wp-trackback.php');
  253.         exit;
  254.     }
  255. }
  256.  
  257. endif;
  258. ?>
  259.